﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CetWin.Phone.Entity;
using System.Collections.Generic;
using System.Linq;
using Phone.Framework.Net;
using Phone.Framework.Threads;
using Phone.Framework;
using System.Threading;
namespace CetWin.Phone.Services
{
    public class WordService : BaseDataService
    {

        public Word GetWord(string wordName)
        {

            return db.Words.FirstOrDefault(e => e.Name == wordName);
        }

        public WordExtesion GetWordExtesion(string wordName)
        {
            var entity = db.WordExtesions.FirstOrDefault(e => e.Name == wordName);
            if (entity == null)
                return AddWordExtesion(wordName);
            return entity;
        }

        public WordExtesion AddWordExtesion(string wordName)
        {
            var entity = new WordExtesion() { Name = wordName };
            db.WordExtesions.InsertOnSubmit(entity);
            Update();
            return entity;
        }

        public void AsyncUpdateNetWord(Word word, Action action)
        {
            WebUtils.DoXMLGet<dict>(string.Format(Constant.DICTCNURL, word.Name),
                    new Action<dict>((dict obj) =>
                    {
                        UIThread.Invoke(() =>
                        {
                            word.Pron = obj.pron;
                            word.Explain = obj.def;
                            word.Audio = obj.audio;
                            ThreadManager.Instance.AddTask(() => { Update(); });
                            action();
                        });
                    }));
        }

        public void AsyncAddNetWord(string wordText, Action<Word> action)
        {
         
            WebUtils.DoGet<dict>(string.Format(Constant.DICTCNURL, wordText), null, DataType.XML,
                new Action<dict>((dict obj) =>
                {
                    if (obj.IsFound)
                    {
                        var word = new Word()
                        {
                            Name = obj.key,
                            Explain = obj.def,
                            Pron = obj.pron,
                            Audio = obj.audio,
                            WordExtesion = new WordExtesion() { Name = obj.key, Comment = string.Empty }
                        };
                        ThreadManager.Instance.AddTask(() =>
                        {
                            if (!db.Words.Any(e => e.Name == obj.key))
                            {
                                db.Words.InsertOnSubmit(word);
                                Update();
                            }
                            action(word);
                        });
                    } else
                        action(null);
                }));
        }
    }
}